package com.yanqu.road.server.cmd.activity.autoreward.rank;

import com.yanqu.road.entity.activity.fivehero.cross.CrossFiveHeroRank;
import com.yanqu.road.entity.activity.fivehero.cross.CrossFiveHeroRankListModel;
import com.yanqu.road.entity.activity.fivehero.cross.CrossFiveHeroUnion;
import com.yanqu.road.entity.activity.fivehero.cross.CrossFiveHeroUnionMember;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.pb.activity.AutoSendRewardProto;
import com.yanqu.road.pb.crossunion.activity.CrossUnionActivityRankDataProto;
import com.yanqu.road.server.manager.activity.fivehero.CrossFiveHeroActivity;
import com.yanqu.road.server.manager.activity.fivehero.CrossFiveHeroMgr;

import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FiveHeroActivityRank extends BaseCrossRank {

    public FiveHeroActivityRank(int activityId) {
        super(activityId);
    }

    @Override
    public AutoSendRewardProto.CrossUserRankMsg.Builder getCrossUserRankMsg() {
        return null;
    }

    @Override
    public AutoSendRewardProto.CrossUnionRankMsg.Builder getCrossUnionRankMsg() {
        return null;
    }

    public AutoSendRewardProto.CrossUserRankMsg.Builder getCrossUserRankMsg(int groupId) {
        CrossFiveHeroActivity activity = CrossFiveHeroMgr.getFiveHeroActivity(activityId,groupId);
        if (activity == null) {
            return null;
        }

        CrossFiveHeroRankListModel userRank = activity.getUserRank();
        if (userRank == null) {
            CrossFiveHeroMgr.getLogger().error("五熊自动发奖没有找到个人榜单");
            return null;
        }
        LinkedList<CrossFiveHeroRank> rankList = userRank.getRankList();

        AutoSendRewardProto.CrossUserRankMsg.Builder msg = AutoSendRewardProto.CrossUserRankMsg.newBuilder();
        msg.setActivityId(activityId);
        int rankNum = 0;
        for (CrossFiveHeroRank rank : rankList) {
            rankNum++;
            long userId = Long.valueOf(rank.getId());
            addUserRankData(msg, eGamePlayerEventType.FiveHeroUserPopularityRank.getValue(), userId, rankNum);
        }
        return msg;
    }

    @Override
    public AutoSendRewardProto.CrossUnionRankMsg.Builder getCrossUnionRankMsg(int groupId) {
        CrossFiveHeroActivity activity = CrossFiveHeroMgr.getFiveHeroActivity(activityId,groupId);
        if (activity == null) {
            return null;
        }

        AutoSendRewardProto.CrossUnionRankMsg.Builder msg = AutoSendRewardProto.CrossUnionRankMsg.newBuilder();
        msg.setActivityId(activityId);

        // 拿阵营排名
        Map<Integer, Integer> campRankMap = new ConcurrentHashMap<>(); // 阵营，排名
        int rankNum = 0;
        LinkedList<CrossFiveHeroRank> campRankList = activity.getCampScoreRankList();
        for (CrossFiveHeroRank campRank : campRankList) {
            rankNum++;
            campRankMap.put(Integer.valueOf(campRank.getId()), rankNum);
        }

        // 拿商会榜
        Map<Integer, CrossFiveHeroRankListModel> campUnionRankMap = activity.getCampUnionRankMap(); // 阵营ID，商会榜
        Map<String, CrossFiveHeroUnion> unionMap = activity.getUnionMap(); // 商会UID，商会报名记录
        for (Map.Entry<Integer, CrossFiveHeroRankListModel> entry : campUnionRankMap.entrySet()) {
            int campId = entry.getKey();
            int campRankNum = campRankMap.getOrDefault(campId, -1);
            int unionRankNum = 0;
            for (CrossFiveHeroRank unionRank : entry.getValue().getRankList()) {
                unionRankNum++;
                CrossFiveHeroUnion union = unionMap.get(unionRank.getId());
                if (union == null) {
                    continue;
                }
                addCampUnionRankData(msg, eGamePlayerEventType.FiveHeroCampUnionPopularityRank.getValue(), campRankNum,
                        unionRank.getId(), unionRankNum, union.getMemberMap());
            }

        }
        return msg;
    }


    private void addCampUnionRankData(AutoSendRewardProto.CrossUnionRankMsg.Builder msg, int conditionType, int campRankNum,
                                      String unionUid, int unionRankNum, Map<Long, CrossFiveHeroUnionMember> memberMap) {

        AutoSendRewardProto.UnionRankMsg.Builder builder = AutoSendRewardProto.UnionRankMsg.newBuilder();
        builder.setConditionType(conditionType);
        builder.setUnionId(unionUid);
        builder.setRank(unionRankNum);
        builder.setCampRank(campRankNum);
        for (CrossFiveHeroUnionMember member : memberMap.values()) {
            builder.addPosList(CrossUnionActivityRankDataProto.CrossUnionMemberPosDataMsg.newBuilder().setUserId(member.getUserId()).setPos(member.getPosition()));
        }
        msg.addData(builder);
    }
}
